package com.menu.web.controller.attribute;

import cn.hutool.core.util.RandomUtil;
import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.page.TableDataInfo;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.enums.Template;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.domain.MenuProductBasicattribute;
import com.menu.system.service.IMenuProductBasicattributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品基础属性模板Controller
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/system/basicattribute")
public class MenuProductBasicattributeController extends BaseController {
    @Autowired
    private IMenuProductBasicattributeService menuProductBasicattributeService;

    /**
     * 查询产品基础属性模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:basicattribute:list')")
    @GetMapping("/list")
    public TableDataInfo list(MenuProductBasicattribute menuProductBasicattribute) {
        startPage();
        List<MenuProductBasicattribute> list = menuProductBasicattributeService.selectMenuProductBasicattributeList(menuProductBasicattribute);
        return getDataTable(list);
    }

    /**
     * 导出产品基础属性模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:basicattribute:export')")
    @Log(title = "产品基础属性模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuProductBasicattribute menuProductBasicattribute) {
        List<MenuProductBasicattribute> list = menuProductBasicattributeService.selectMenuProductBasicattributeList(menuProductBasicattribute);
        ExcelUtil<MenuProductBasicattribute> util = new ExcelUtil<MenuProductBasicattribute>(MenuProductBasicattribute.class);
        util.exportExcel(response, list, "产品基础属性模板数据");
    }

    /**
     * 获取产品基础属性模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:basicattribute:query')")
    @GetMapping(value = "/{basicattributeId}")
    public AjaxResult getInfo(@PathVariable("basicattributeId") Long basicattributeId) {
        return success(menuProductBasicattributeService.selectMenuProductBasicattributeByBasicattributeId(basicattributeId));
    }

    /**
     * 新增产品基础属性模板
     */
    @PreAuthorize("@ss.hasPermi('system:basicattribute:add')")
    @Log(title = "产品基础属性模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuProductBasicattribute menuProductBasicattribute) {
        if (menuProductBasicattributeService.selectMenuProductBasicattributeByType(menuProductBasicattribute.getBasicattributeLabel()) != null) {
            return AjaxResult.error(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ?"产品基础属性名称已存在":"The product basic attribute name already exists");
        }

        menuProductBasicattribute.setBasicattributeProd(Template.BA + RandomUtil.randomStringWithoutStr(8, "0123456789"));
        return toAjax(menuProductBasicattributeService.insertMenuProductBasicattribute(menuProductBasicattribute));
    }

    /**
     * 修改产品基础属性模板
     */
    @PreAuthorize("@ss.hasPermi('system:basicattribute:edit')")
    @Log(title = "产品基础属性模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuProductBasicattribute menuProductBasicattribute) {


        return toAjax(menuProductBasicattributeService.updateMenuProductBasicattribute(menuProductBasicattribute));
    }

    /**
     * 删除产品基础属性模板
     */
    @PreAuthorize("@ss.hasPermi('system:basicattribute:remove')")
    @Log(title = "产品基础属性模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{basicattributeIds}")
    public AjaxResult remove(@PathVariable Long[] basicattributeIds) {
        return toAjax(menuProductBasicattributeService.deleteMenuProductBasicattributeByBasicattributeIds(basicattributeIds));
    }
}
